package cn.lql.org.service.impl;

import cn.lql.basic.service.impl.BaseServiceImpl;
import cn.lql.basic.util.PageList;
import cn.lql.org.domain.Department;
import cn.lql.org.mapper.DepartmentMapper;
import cn.lql.org.query.DepartmentQuery;
import cn.lql.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    // 1.基本的CRUD实现通过继承得到

    // 2.自己写扩展的实现


    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);// 返回自增id
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        // 封装dirPath
        String dirPath = "";
        // 新增顶级部门
        if(department.getParent() == null){
            dirPath = "/"+department.getId();
        }else{ // 新增非顶级部门
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            dirPath = parentDept.getDirPath() + "/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    @Override
    public List<Department> queryTree() {
        return departmentMapper.loadTree();
    }
}
